//
//  informationsharewebview.m
//  HYPublicEdition
//
//  Created by apple on 2017/3/22.
//  Copyright © 2017年 弘扬公众版. All rights reserved.
//

#import "informationsharewebview.h"
#import "Config.h"
#import "SVProgressHUD.h"
#import "XHAppDelegate.h"
#import "BaseController.h"
//分享
#import <UMSocialCore/UMSocialCore.h>
#import <UShareUI/UShareUI.h>
@interface informationsharewebview ()
    @property (weak, nonatomic) IBOutlet UIWebView *webView;
@property  (nonatomic,assign) NSString * weburl;

@end

@implementation informationsharewebview

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.webView.backgroundColor = [UIColor whiteColor];
    if (self.url.length) {
        NSString * urls = [self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL * url = [URLTool webURLStringWithPathString:urls token:[UserDefaultsTool stringForKey:XHToken]];
        //缓存web清除
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        if (url) {//清除所有cookie
            NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
            for (int i = 0; i < [cookies count]; i++) {
                NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
            }
        }
        self.weburl = url.absoluteString;
        [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
        
    }
    
    
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 40, 40);
    [button setTitle:@"分享" forState:UIControlStateNormal];
    button.font = [UIFont systemFontOfSize:15.0];
    [button addTarget:self action:@selector(showAllQuestions:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * items = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = items;


    
}
-(void)showAllQuestions:(UIButton *)sender
{
    [self getUMengwindow];
}
#pragma mark 友盟
// 调用分享面板
-(void)getUMengwindow{
    
    //定制自己的分享面板预定义平台
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_WechatFavorite),@(UMSocialPlatformType_Qzone)]];
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        // 根据获取的platformType确定所选平台进行下一步操作
        [self shareWebPageToPlatformType:platformType];
    }];
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    //    NSString* thumbURL =  @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"欢迎使用【龙岗爱维】APP" descr:@"用于爱维艾夫集团龙岗妇科医院用户使用，可以进行预约挂号，报告单查看，充值缴费，优惠促销等等。" thumImage:[UIImage imageNamed:@"51252.png"]];
    //    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"测试" descr:@"dsdgasdg" thumImage:nil];
    //    //设置网页地址
    shareObject.webpageUrl = self.weburl;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    messageObject.title = self.title;
    messageObject.text = self.text;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
        //        [self setAlertViewWithMessage:error];
//        NSLog(@"%@",error);
    }];
}

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
}
    
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.webView.userInteractionEnabled = YES;
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
